package com.heihu577.controller.child;

import com.aliyun.oss.OSS;
import com.heihu577.bean.AliyunOSSBean;
import com.heihu577.bean.MarkdownFile;
import com.heihu577.controller.MyAppController;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextArea;
import javafx.stage.FileChooser;
import lombok.Data;
import lombok.Setter;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Vector;

@Data
public class MarkdownResultController {
    // 准备输出到日志中
    private MyAppController myAppController;
    private String url = "https://markdown.com.cn/editor/";
    private HostServices hostServices;
    private OSS ossClient;
    private AliyunOSSBean aliyunOSSBean;
    private MarkdownFile markdownFile;

    @Setter
    private Vector<String> ossUploadedFile = new Vector<>();

    @FXML
    public TextArea markdownContent;

    @FXML
    void deleteMe(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("确认删除 [ " + markdownFile.getFile().getAbsolutePath() + " ] 中的图片吗？");
        alert.setHeaderText(null);
        alert.setContentText("您确定要删除这些文件吗？");
        // 显示弹窗并等待用户的响应
        ButtonType result = alert.showAndWait().orElse(ButtonType.CANCEL);
        // 如果用户点击了“确定”按钮，则执行删除操作
        if (result == ButtonType.OK) {
            // 在这里添加实际的删除逻辑
            aliyunOSSBean.deleteFileByList(markdownFile, myAppController, ossUploadedFile);
        }
    }

    @FXML
        // 打开 Markdown
    void openURL(ActionEvent event) throws URISyntaxException, IOException {
        // 复制到剪贴板
        Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        systemClipboard.setContents(new java.awt.datatransfer.StringSelection(markdownContent.getText()), null);
        // 打开网址
        java.awt.Desktop.getDesktop().browse(new java.net.URI(url));
    }

    @FXML
        // 文件另存为功能
    void saveAs(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Save File");
        // 设置文件自动保存类型
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Markdown", "*.md"));
        fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Markdown", "*.md"));
        //显示文件选择对话框
        File file = fileChooser.showSaveDialog(((javafx.scene.control.Button) event.getSource()).getParent().getScene().getWindow());
        if (file != null) {
            // 用户选择了文件并且点击了保存，现在可以将数据保存到文件中
            String dataToSave = markdownContent.getText();
            Path path = file.toPath();
            try {
                // 使用Files类的write方法将数据写入文件
                Files.write(path, dataToSave.getBytes(StandardCharsets.UTF_8));
                myAppController.printToLog("[+] 文件 " + path + " 保存成功！");
            } catch (IOException e) {
                e.printStackTrace();
                myAppController.printToLog("[+] 文件 " + path + " 保存失败！");
            }
        }
    }
}
